﻿using Bug315.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Bug315
{
    // Singleton...
    public class BugService
    {
        // Private Vars...
        private int _counter;

        //holds all bugs 
        private List<WorkItem> _allBugs;

        // internal variable to holde the BugService
        private static BugService _instance = null;

        public static BugService Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new BugService();
                }
                return _instance;
            }
        }


        // Constructor...
        private BugService()
        {

            _counter = 10;
            // Create a list of object (Bug, TaskToDo)
            _allBugs = new List<WorkItem>() {
                new Bug(){ Id=1, Title="audio not working", Priority=1, DateCreated=DateTime.Now, StepsToReproduce="click the sound icon"},
                new Bug(){ Id=2, Title="The dropdown is empty", Priority=3, DateCreated=DateTime.Now, StepsToReproduce="choose state, then city"},
                new TaskToDo(){ Id=3,Title="Call client XYZ", Priority=3, DateCreated=DateTime.Now.AddDays(-5), Description="Follow up deal ABC"},
                new TaskToDo(){ Id=4,Title="Purchase strong coffee", Priority=1, DateCreated=DateTime.Now.AddHours(-5), Description="Colmbian blend please"}
            };


        }


        // Public Counter prop - talks to the Private _counter
        public int Counter
        {
            get
            {
                return _counter;
            }
            set
            {
                _counter = value;
            }
        }

        public void Increment()
        {
            _counter++;
        }
        //fetch all the bugs 

        public List<WorkItem> fetchAllBugs()
        {
            return _allBugs;
        }

        // post to list from CREATE

        public void Create(Bug createBug)
        {
            _allBugs.Add(createBug);

        }

        // fetch the current item - on userclick
        public Bug Update(int id)
        {
            var updateBug = _allBugs.FirstOrDefault(t => t.Id == id);
            return (Bug)updateBug;
        }
        public void SaveChanges(int id, WorkItemUpdateVm updateVm, int priorityAssign)
        {
            Bug jelly = (Bug)_allBugs.FirstOrDefault(t => t.Id == id);
            jelly.Title = updateVm.Title;
            jelly.DateCreated = updateVm.DateCreated;
            jelly.Priority = priorityAssign;
            jelly.StepsToReproduce = updateVm.StepsToReproduce;
            
                
        }
        public void Delete(int id)
        {
            WorkItem dBug = _allBugs.FirstOrDefault(d => d.Id == id);
            _allBugs.Remove(dBug);
        }
    }
}